package com.sy.service.impl;

import com.sy.entity.ThumbsUp;
import com.sy.service.ThumbsUpService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.sy.constant.BusinessConstant.*;

@Service
public class ThumbsUpServiceImpl extends BaseServiceImpl implements ThumbsUpService {
    @Override
    public Integer addOrCancelThumbsUp(ThumbsUp thumbsUp) {
        if (Objects.isNull(thumbsUp.getUserId())) {
            buildError(HttpStatus.NOT_FOUND, "用户未登录");
        }
        if (Objects.isNull(thumbsUp.getVideoId())) {
            buildError(HttpStatus.NOT_FOUND, "视频id不能为空");
        }
        ThumbsUp curThumbsUp = thumbsUpMapper.getThumbsUpInfo(thumbsUp.getUserId(), thumbsUp.getVideoId());
        if (Objects.isNull(curThumbsUp)) {
            thumbsUpMapper.addLike(thumbsUp.getUserId(), thumbsUp.getVideoId());
            //视频点赞量加一
            videoMapper.changeThumbsUps(thumbsUp.getVideoId(), THUMBS_UP_INCR);
            return SUCCESS_THUMBS_UP;
        } else {
            thumbsUpMapper.deleteLike(thumbsUp.getUserId(), thumbsUp.getVideoId());
            //视频点赞量减一
            videoMapper.changeThumbsUps(thumbsUp.getVideoId(), CANCEL_THUMBS_UP_INCR);
            return CANCEL_THUMBS_UP;
        }
    }
}
